﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TB.DataModel.DTO;
using TB.BLL;
using System.Threading;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.Script.Services;

public partial class Display : System.Web.UI.Page
{
    public MessageHandler MessageHandler { get; set; }

    public string msgsList { get; set; }
    private IList<Message> msgs;
    private int currentMsgPos;
    private System.Threading.Timer timer;



    protected void Page_Load(object sender, EventArgs e)
    {
       // label1.Text = "bla";
       // Message msg = new Message(Author.Value, Email.Value, TextArea1.InnerText);

        msgsList = FillMessages();


    }

    [WebMethod(EnableSession = true)]
    [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)] 
    public string RefillMessages()
    {
        return FillMessages();
    }

    private string FillMessages()
    {
        var msgs = MessageHandler.Instance.GetAllMessages();
        msgs = MessageHandler.Instance.ShufelMessages(msgs);

        return new JavaScriptSerializer().Serialize(msgs);
    }

    ////protected void Button1_Click(object sender, EventArgs e)
    ////{
    ////   // label1.Text = "bla";
    ////  //  Message msg = new Message(Author.Value, Email.Value, TextArea1.InnerText);
        
    ////    msgs = SessionData.MessageHandler.GetAllMessages();
    ////    msgs = SessionData.MessageHandler.ShufelMessages(msgs);

    ////    msgsList = new JavaScriptSerializer().Serialize(msgs);

    ////    //timer = new System.Threading.Timer(new TimerCallback(DoSomething), null, 0, 10000);

    ////    //Thread.CurrentThread.Join();
    ////}

    //private void DoSomething(object obj)
    //{
    //    if (currentMsgPos < msgs.Count)
    //    {
    //        Message msg = msgs[currentMsgPos++];

    //       // label1.Text = string.Format("bla {0}", currentMsgPos);
    //        //TextArea1.InnerText = msg.Content + currentMsgPos;
    //    }
    //    else
    //    {
    //        timer.Change(Timeout.Infinite, Timeout.Infinite);
    //    }
    //}

}